package org.jeecg.modules.demo.socket.until;

import com.aliyun.oss.HttpMethod;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;
import com.aliyun.oss.model.ObjectMetadata;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.demo.upload.entity.OOS;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.Date;


@Slf4j
public class FileSave {


//    ApplicationHome applicationHome = new ApplicationHome(this.getClass());
//     String Path =applicationHome.getDir().getParentFile()
//            .getParentFile().getAbsolutePath()+"\\src\\main\\resources";
    String Path=System.getProperty("user.home") + "/Downloads";
    public  String fileSave(MultipartFile file){
        log.info("无误");
        try {
            OSS ossClient = new OSSClientBuilder().build(OOS.ALIYUN_OSS_ENDPOINT, OOS.ALIYUN_OSS_ACCESSKEYID, OOS.ALIYUN_OSS_ACCESSKEYSECRET);
            log.info("运行到FileSave");
            //        得到初始图片名字originalFileName
            String originalFileName = file.getOriginalFilename();

        /*
        文件存储库位置
         */
            File file1 =new File(Path,originalFileName);
            file.transferTo(file1);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

            // oss中的文件夹名
            String objectName = sdf.format(new Date()) + "/" + originalFileName;

            // 创建上传文件的元信息，可以通过文件元信息设置HTTP header(设置了才能通过返回的链接直接访问)。
            ObjectMetadata objectMetadata = new ObjectMetadata();
            objectMetadata.setContentType("image/jpg");
            objectMetadata.setContentDisposition("inline");

            //缓存文件上传至OSS
            InputStream inputStream = Files.newInputStream(file1.toPath());
            ossClient.putObject(OOS.ALIYUN_OSS_BUCKETNAME, objectName, inputStream, objectMetadata);

            // 设置URL过期时间为1小时。
            Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000*24*2000*1000);


            // 设置样式，样式中包含文档预览参数。
            GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(OOS.ALIYUN_OSS_BUCKETNAME, objectName, HttpMethod.GET);
            req.setExpiration(expiration);
            URL signedUrl = ossClient.generatePresignedUrl(req);
            log.info("无误");
            String url=signedUrl.toString();
            ossClient.shutdown();
            file1.delete();
            return url;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
